package com.tpfinal.agenda.view.renderers;

import com.tpfinal.agenda.model.Company;
import com.tpfinal.agenda.model.Contact;
import com.tpfinal.agenda.model.Person;

public class ContactViewAdapter {

	private Contact adpatee;

	private boolean visible;

	public ContactViewAdapter(Contact adaptee) {
		this.adpatee = adaptee;
	}

	public Cell[] getForTable() {
		Cell[] res = new Cell[13];

		res[ContactsTableModel.COLUMN_IDX_NOMBRE_RAZON] = new Cell(this,
				this.adpatee.getName());
		res[ContactsTableModel.COLUMN_IDX_TIPO] = new Cell(this, this.adpatee
				.getClass().getSimpleName());
		res[ContactsTableModel.COLUMN_IDX_DIRECCION] = new Cell(this,
				this.adpatee.getAddress());

		res[ContactsTableModel.COLUMN_IDX_CP] = new Cell(this,
				String.valueOf(this.adpatee.getCityInfo().getZip()));
		res[ContactsTableModel.COLUMN_IDX_CITY] = new Cell(this, this.adpatee
				.getCityInfo().getCity());
		res[ContactsTableModel.COLUMN_IDX_STATE] = new Cell(this, this.adpatee
				.getCityInfo().getState());
		res[ContactsTableModel.COLUMN_IDX_DDN] = new Cell(this,
				String.valueOf(this.adpatee.getCityInfo().getDdn()));

		res[ContactsTableModel.COLUMN_IDX_TELEFONO] = new Cell(this,
				String.valueOf(this.adpatee.getPhone()));
		res[ContactsTableModel.COLUMN_IDX_EMAIL] = new Cell(this,
				this.adpatee.getEmail());

		if (this.adpatee instanceof Person) {
			res[ContactsTableModel.COLUMN_IDX_TELEFONO_MOVIL] = new Cell(this,
					String.valueOf(((Person) this.adpatee).getCellphone()));
			res[ContactsTableModel.COLUMN_IDX_FECHA_NACIMIENTO] = new Cell(
					this,
					(((Person) this.adpatee).getBirthday() != null) ? ((Person) this.adpatee)
							.getBirthday().toString() : "");
		} else {
			res[ContactsTableModel.COLUMN_IDX_WEB] = new Cell(this,
					((Company) this.adpatee).getUrl());
			res[ContactsTableModel.COLUMN_IDX_RUBROS] = new Cell(this,
					((Company) this.adpatee).getBusinessSector());
		}

		return res;
	}

	public Contact getAdaptee() {
		return adpatee;
	}

	public boolean isVisible() {
		return visible;
	}

	public void setVisible(boolean visible) {
		this.visible = visible;
	}

	public class Cell {

		private ContactViewAdapter parent;
		private String value;

		public Cell(ContactViewAdapter parent, String value) {
			this.parent = parent;
			this.value = value;
		}

		@Override
		public String toString() {
			return this.value;
		}

		public ContactViewAdapter getParent() {
			return this.parent;
		}

	}

}
